package org.example.jdbc.bank.dao;

import org.example.jdbc.bank.bean.Account;
import org.example.jdbc.bank.bean.Record;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

/**
 * Company 源辰信息
 *
 * @author navy
 * @date 2024/4/23
 * Email haijunzhou@hnit.edu.cn
 */
@Repository
public class RecordDaoImpl implements RecordDao{

    @Resource
    JdbcTemplate jdbcTemplate;

    @Override
    public void insert(Record t) {
        jdbcTemplate.update("insert into bank_oprecord values(default,?,?,null,null,null)"
                ,t.getAid(),t.getOpmoney());
    }

    @Override
    public List<Record> selectByAccountId(Integer id) {
        // 表字段与类属性完全一致, 那么可以
        // 使用 BeanPropertyRowMapper 实现自动映射
        return jdbcTemplate.query("select * from bank_oprecord where aid=?",
                new BeanPropertyRowMapper<Record>(Record.class), id);
    }
}
